﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;

public partial class ajax_getwork : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["id_user"] != null && Session["permission"] != null
            && (((int)Session["permission"]) >= 2))
        {
            try
            {
                int leaderId = Int32.Parse(Session["id_user"].ToString());
                int workId = Int32.Parse(Request.QueryString["id"]);
                string sql = "SELECT projectId FROM work WHERE id=" + comm.to_sql(workId.ToString(), "number");
                DataTable dtTable = dal.SelectTable(sql);
                int projectId = 0;
                if (dtTable.Rows.Count == 1)
                {
                    projectId = Int32.Parse(dtTable.Rows[0]["projectId"].ToString());
                }
                else
                    throw new Exception("FAIL");

                #region check leader is enable to access this project
                SqlParameter[] listParams = new SqlParameter[3];
                //-- param 1: @userId
                SqlParameter param = new SqlParameter("@userId", leaderId);
                param.SqlDbType = SqlDbType.Int;
                param.Direction = ParameterDirection.Input;
                listParams[0] = param;

                //-- param 2: @projectId
                param = new SqlParameter("@projectId", projectId);
                param.SqlDbType = SqlDbType.Int;
                param.Direction = ParameterDirection.Input;
                listParams[1] = param;

                //-- param 3: @isAccess
                param = new SqlParameter();
                param.ParameterName = "@isAccess";
                param.SqlDbType = SqlDbType.Int;
                param.Direction = ParameterDirection.Output;
                listParams[2] = param;

                dal.ExecuteSP("proCheckAccessProject_GET", listParams);
                int enable = Int32.Parse(listParams[2].Value.ToString());
                if (enable == 0)
                    throw new Exception("FAIL");
                #endregion

                #region check leader is enable to add/edit work to project
                if (((int)Session["permission"]) >= 4)
                {
                    sql = " SELECT permission FROM UserProject " +
                          " WHERE userId=" + comm.to_sql(leaderId.ToString(), "number") +
                          " AND projectId=" + comm.to_sql(projectId.ToString(), "number");
                    dtTable = dal.SelectTable(sql);
                    if (dtTable.Rows.Count == 1)
                    {
                        int permission = Int32.Parse(dtTable.Rows[0]["permission"].ToString());
                        if (permission >= 5)
                            throw new Exception("FAIL");
                    }
                    else
                        throw new Exception("FAIL");
                }
                #endregion

                #region read information of work
                sql = "SELECT catId,userId,[content],description,convert(varchar,start,103) as 'start'," +
                      " convert(varchar,[end],103) as 'end',priority FROM work " +
                      " WHERE id=" + comm.to_sql(workId.ToString(), "number");
                dtTable = dal.SelectTable(sql);
                if (dtTable.Rows.Count != 1)
                    throw new Exception("FAIL");
                #endregion

                //'content'
                //'des'
                //'catalogId'
                //'priority'
                //'userId'
                //'start'
                //'end'
                #region send work to client
                //string json = "{";
                //json += "'content':'" + dtTable.Rows[0]["content"].ToString() + "',";
                //json += "'description':'" + dtTable.Rows[0]["description"].ToString() + "',";
                //json += "'catId':'" + dtTable.Rows[0]["catId"].ToString() + "',";
                //json += "'priority':'" + dtTable.Rows[0]["priority"].ToString() + "',";
                //json += "'userId':'" + dtTable.Rows[0]["userId"].ToString() + "',";
                //json += "'start':'" + dtTable.Rows[0]["start"].ToString() + "',";
                //json += "'end':'" + dtTable.Rows[0]["end"].ToString()+"'";
                //json += "}";
                string json = JsonConvert.SerializeObject(dtTable.Rows[0].Table);
                throw new Exception(json);
                #endregion
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
            }
        }
    }
}
